/**
 * Created by yangfan on 2016/11/22.
 */
define(['config/appregister',
    "i18n",
    "business/services/constant",
    "business/myResume/services/httpServices",
    "business/services/utils",
    "business/services/components",
     "business/myResume/services/basicInfoComp",
     "business/myResume/services/jobIntensionComp",
    "business/myResume/services/workExpresComp",
    "business/myResume/services/educationComp",
    "business/myResume/services/projectExpressionComp",
    "business/services/filter",
    "business/services/dateComp",
    "business/services/regionService"],function(app,i18n,constant,httpServices,util){
    app.controller('myResumeMineCtrl', function($scope,$state,$location,$rootScope,$filter) {
        $scope.i18n = i18n;
        $scope.curI18n = i18n.resume;
        $scope.$parent.isMyResume = false;
        $scope.personSrc = "them/images/resume-person-pic.png";

        $scope.$on('$stateChangeStart', function(event, toState, toParams, fromState, fromParams, options){
            var a  = 1 ;
            if(toState.name.indexOf("resume.")>-1 &&toState.name!="resume.myResumePreview" ){
                $scope.$parent.isMyResume = true;
            }
        });
        $scope.showBox = {
            jobIntension:false,
            basic:false,

        };
        $scope.edit = function(key){
            if($scope.showBox.hasOwnProperty(key)){
                $scope.showBox[key] = true;
            }
        };
        //设置简历公开
        $scope.clickSwitch = function(){
            /*if($('#onoffswitch').prop('checked')){
                $('#onoffswitch').prop('checked',false);
            }else{
                $('#onoffswitch').prop('checked','checked');
            }*/
	        if($scope.open == 0){
	        	$scope.open = 1
	        }else if($scope.open == 1){
	        	$scope.open = 0;
	        }
	        var req = {
                type:"PUT",
                uri:"updateOpen",
                data:{
                    cvdId:$scope.basicInfo.cvdId,
                    open:$scope.open
                }
            };
            httpServices.getCvdatabases(req).success(function(response){
                $scope.$apply(function(){
                    /*$("#onoffswitch").prop('checked',$scope.basicInfo.open);*/
                })
            });
        };
        
        //是否展示编辑基本信息
        $scope.showBasicEdit = false;
        var utils = {};
        utils.getJobTime = function(time){

        };
        utils.getSex = function(id){
            if(typeof parseInt(id)!="number"){
                return
            };
            if(id == 0){
                return "男"
            }else if(id == 1){
                return "女"
            }
        };
        utils.getMarried = function(status){
            if(typeof parseInt(id)!="number"){
                return
            };
            if(status == 0){
                return "未婚"
            }
            if(status == 1){
                return "已婚"
            }
            if(status == 2){
                return "离异"
            }
        };
        utils.getYear = function(time){
            if(typeof time != "string" ){
                return
            }
            var oldYear = parseInt(time.split(0,time.indexOf("-")));
            var thisYear = parseInt(new Date().getFullYear());
            return thisYear - oldYear
        };
        //是否新增
        $scope.isInitAddNew = {
            workExpression:false,
            educationExpression:false,
            projectExpression:false
        };


        //获取简历信息
        function  get(){
            var req = {
                type:"GET",
                uri:"get",
                data:{
                    userId:$rootScope.userLoginInfo.data.userId
                }
            };
            httpServices.getCvdatabases(req).success(function(response){
                var data = response.data;
                $scope.$apply(function(){
                    $scope.userInfoModel = response.data ;
                	$scope.user = $rootScope.userLoginInfo.data.userId;
                	$scope.logoUrl = response.data.logoUrl;
                    $scope.open = response.data.open;
                    //基本信息
                    $scope.basicInfo = {
                        name: data.name,
                        sex: data.sex,
                        birthday: util.clearDateSpace(data.birthday) ,

                        jobTime:data.jobTime,
                        workYear:data.workYear,
                        phone:data.phone,
                        //
                        jobStatus:data.jobStatusObject,
                        email:data.email,
						cvdId:data.cvdId,
                        //现居住地 传市级单位
                        lregionObject:{
                            region:data.lregionObject,
                            area:data.lareaObject,
                        },
                        //户口所在地
                        htObject:{
                            provice:data.htprovinceObject,
                            region:data.htregionObject,
                            area:data.htareaObject
                        },
                        //婚姻状态
                        maritalStatus:data.maritalStatus,
                        //证件号
                        card:{
                            cardId:data.cardId,
                            cardNumber:data.cardNumber
                        },
                        //政治面貌
                        politicalStatusObjec:data.politicalStatusObject,
                        language:data.clList,
                        //详细居住地址
                        expectWorkPlace:data.expectWorkPlace,
                        introduction:data.introduction,
                    };
                    //求职意向
                    $scope.jobIntention = {
                        salary:!!data.jobIntentionVo?data.jobIntentionVo.salaryObject:null,
                        //职能
                        jobConf:!!data.jobIntentionVo?data.jobIntentionVo.cvdFdcList:null,
                        //到岗时间
                        jobInArrivalTime:!!data.jobIntentionVo?data.jobIntentionVo.arrivalTime:null,
                        //行业
                        jobInIndustryConf:!!data.jobIntentionVo?data.jobIntentionVo.cvdGuildList:null,
                        //工作性质
                        jobNature:!!data.jobIntentionVo?data.jobIntentionVo.dc:null,
                        //期望工作地
                        jobInRegionIntension:!!data.jobIntentionVo?data.jobIntentionVo.cvdEwpAddressList:null
                    };



                    //工作经历
                    if(!data.workList){
                        //show 新增工作经验
                    }else {
                        var arrWorkExpression = [],obj;
                        data.workList.forEach(function(item,index,array){
                            obj = {
                                weId:item.weId,
                                cvdId:item.cvdId,
                                startValue:item.startTime,
                                endValue:item.endTime,
                                companyName:item.companyName,
                                //行业
                                workExpreIndustryConf:item.dc,
                                //规模
                                companyScale:item.scale,

                                //职能
                                funcd:item.funcd,
                                //公司性质
                                jobDc:item.natureDc,
                                //工作性质
                                jobNature:item.jobDc,

                                // 部门
                                department:item.department,
                                //薪资水平
                                salary:item.salaryObject,
                                introduction:item.introduction
                            };
                            arrWorkExpression.push(obj);
                        });
                        $scope.workExpression = arrWorkExpression;
                        if($scope.workExpression.length == 0){
                            $scope.isInitAddNew.workExpression = true;
                        }
                    }

                    //教育经历
                    if(!data.edueList){
                        //show 新增教育经历
                    }else {
                        var arrEdu = [],obj;
                        data.edueList.forEach(function(item,index,array){
                            obj = {
                                cvdId:item.cvdId,
                                eeId:item.eeId,
                                degreeId:item.degreeId,
                                startValue:item.startTime,
                                endValue:item.endTime,
                                school:item.eduName,
                                majorConf:item.profe,
                                overseas:item.overseas,
                                introduction:item.introduction
                            };
                            arrEdu.push(obj);
                        })
                        $scope.educationExpression = arrEdu;
                    }

                    //项目经验
                    if(!data.peList){
                        //show 新增项目经验
                    }else {
                        var arrProject = [],obj;
                        data.peList.forEach(function(item,index,array){
                            obj = {
                                cvdId:item.cvdId,
                                peId:item.peId,
                                companyName:item.companyName,
                                startValue:item.startTime,
                                endValue:item.endTime,
                                peName:item.peName,
                                roleName:item.roleName,
                                introduction:item.introduction
                            };
                            arrProject.push(obj);
                        });
                        $scope.projectExpression = arrProject;
                    }
                    //获取站内作品
                    getBlogList(2);
                });


            }).error(function(response){})

        }
        get();
        $scope.$on("reGetResumeInfo",function(a,obj){
            if(toString.call(obj) == "[object Object]"){
                var key1 = obj.key1,key2 = obj.key2,type = obj.type;
                var types = parseInt(type);
                if(toString.call(key1) != "[object String]" || toString.call(key2) != "[object String]" || toString.call(types) != "[object Number]"){
                    return
                }
                $scope[key1][key2] = false;
                if(type == 0){
                    get();
                }
            }
        });
        $scope.$on("backToResume",function(a,obj){
            if(toString.call(obj) == "[object Object]"){
                var key = obj.key,type = obj.type;
                var types = parseInt(type);
                if(toString.call(key) != "[object String]" && toString.call(types) != "[object Number]"){
                    return
                }
                if($scope.showBox.hasOwnProperty(key)){
                    $scope.showBox[key] = false;
                }
                if(type == 0){
                    get();
                }
            }
        });

        //删除工作经验
        function deleteWorkExperience(weId){
          var req = {
              type:"DELETE",
              uri:"deleteWorkExp",
              data:{
                  weId:weId
              }
              };
            httpServices.getCvdatabases(req).success(function(response){
                if(response.msg = "success"){
                    get();
                }
            }).error(function(){})
        };
        //删除项目经验
        function deleteProjectExperience(peId){
            var req = {
                type:"DELETE",
                uri:"deleteProExp",
                data:{
                    peId:peId
                }
            };
            httpServices.getCvdatabases(req).success(function(response){
                if(response.msg = "success"){
                    get();
                }
            }).error(function(){})
        };
        //删除教育经历
        function deleteEduExperience(eeId){
            var req = {
                type:"DELETE",
                uri:"deleteEduExp",
                data:{
                    eeId:eeId
                }
            };
            httpServices.getCvdatabases(req).success(function(response){
                if(response.msg = "success"){
                    get();
                }
            }).error(function(){})
        };

        $scope.editJobIntention = function(){
            $scope.showBox.jobIntension = true;
        };
        $scope.editBasicInfo = function(){
            $scope.showBox.basic = true;
        };
        //工作经验 展示判断
        $scope.showWorkexperience = {
            showView:[],
            showForm:[]
        };
        //教育经历 展示判断
        $scope.showEducationExpression = {
            showView:[],
            showForm:[]
        };
        //项目经验 展示判断
        $scope.showProjectExpression = {
            showView:[],
            showForm:[]
        };
        //工作经验 绑定事件
        $scope.addNewWorkExpression = function(){
            $scope.isInitAddNew.workExpression = true;
        };
        $scope.editWorkexperience = function(index){
            $scope.showWorkexperience.showForm = [];
            $scope.showWorkexperience.showForm[index] = true;
        };
        $scope.deleteWorkexperience = function(weId){
            deleteWorkExperience(weId);
        };

        //教育经历 绑定事件
        $scope.addNewEducationExpression = function(){
            $scope.isInitAddNew.educationExpression = true;
        };
        $scope.editEducationExpression = function(index){
            $scope.showEducationExpression.showForm = [];
            $scope.showEducationExpression.showForm[index] = true;
        };
        $scope.deleteEducationExpression = function(eeId){
            deleteEduExperience(eeId);
        };
        //项目经验 绑定事件
        $scope.addNewProjectExpression = function(){
            $scope.isInitAddNew.projectExpression = true;
        };
        $scope.editProjectExpression = function(index){
            $scope.showProjectExpression.showForm = [];
            $scope.showProjectExpression.showForm[index] = true;
        };
        $scope.deleteProjectExpression = function(peId){
            deleteProjectExperience(peId);
        };
        $scope.blogList = [];
        $scope.newBlogList = [];
		$scope.selectedBlog = [];
		$scope.allBlog = [];
        function getInfoFill (){
            $scope.infoFill = [
                {title:"基本资料",logoSrc:"them/images/resume-baseInfo-pic.png",isFill:!!$scope.userInfoModel.birthday ?true:false},
                {title:"求职意向",logoSrc:"them/images/resume-airplain-pic.png",isFill:!!$scope.userInfoModel.jobIntentionVo?true:false},
                {title:"工作经验",logoSrc:"them/images/resume-expericence-pic.png",isFill:!!$scope.userInfoModel.workList?true:false},
                {title:"教育经历",logoSrc:"them/images/resume-eduexpericence-pic.png",isFill:!!$scope.userInfoModel.edueList?true:false},
                {title:"项目经验",logoSrc:"them/images/resume-projectexpreience-pic.png",isFill:!!$scope.userInfoModel.peList>0?true:false},
                {title:"站内作品",logoSrc:"them/images/resume-netinnerwork-pic.png",isFill:$scope.blogList.length>0?true:false}
            ];
            // init show tabItem
            if(!$scope.userInfoModel.birthday){
                $scope.showBox.basic = true ;
            }
            if(!$scope.userInfoModel.jobIntentionVo){
                $scope.showBox.jobIntension = true ;
            }
        }
        function  getBlogList(type){
            var req = {
                type:"GET",
                uri:"getList",
                data:{
                    type:type,
                    pageNo:1,
                    pageSize:10
                }
            };
            httpServices.userServices(req).success(function(response){
                $scope.$apply(function(){
                    if(type == "0,2"){
                	//console.log(response.data.list[0].blogId)
                        $scope.isAddNewBlog = true;
                        $scope.newBlogList = response.data.list;
                        for(var i = 0;i < response.data.list.length;i++){
                        	$scope.allBlog.push(response.data.list[i].blogId);
                        }
                     //   console.log($scope.allBlog)
                    	for(var i = 0;i < $scope.allBlog.length;i++){
                    		for(var j = 0;j < $scope.selectedBlog.length;j++){
                    			if($scope.allBlog[i] == $scope.selectedBlog[j]){
                    				$scope.allBlog.splice(i,1);
                    			}
                    		}
                    	}
                    	return;
                    }else if (type ==2){
						$scope.blogList = response.data.list;
						for(var i = 0;i < response.data.list.length;i++){
							$scope.selectedBlog.push(response.data.list[i].blogId);
						}
                        getInfoFill ()

                    }
                })
            });
        };
        
        function deleteBlog(ids){
        	var req = {
                type:"GET",
                uri:"delStationWorks",
                data:{
                    blogIds:ids
                }
            };
            httpServices.userServices(req).success(function(response){
                getBlogList(2);
            })
        }
        	

        function updateStationWorks (ids){
            var req = {
                type:"GET",
                uri:"updateStationWorks",
                data:{
                    blogIds:ids
                }
            };
            httpServices.userServices(req).success(function(response){
            	getBlogList(2);
            })
        };
        $scope.isAddNewBlog = false;
        $scope.updateStationWorks = function(){
            //新增博文
            $scope.isAddNewBlog = true;
            getBlogList("0,2")
        };
        
        $scope.cancelAddNewBlog = function(){
            $scope.isAddNewBlog = false;
            $scope.selectedBlog = [];
        };
        $scope.hideAddNewBlog = function(){
            $scope.isAddNewBlog = false;
        };
        $scope.addBlog = [];
        $scope.cancelBlog = [];
        $scope.checkCheckBox = function(event,item){
        	//console.log(event.target.checked)
            if(event.target.checked){
                for(var i = 0;i < $scope.allBlog.length;i++){
                	if($scope.allBlog[i] == item.blogId){
                		if($scope.addBlog.indexOf(item.blogId) == -1){
                			$scope.addBlog.push(item.blogId);
                			return;
                		}
                	}
                }
            }else{
            	for(var i = 0;i < $scope.selectedBlog.length;i++){
            		if($scope.selectedBlog[i] == item.blogId){
            			if($scope.cancelBlog.indexOf(item.blogId) == -1){
            				$scope.cancelBlog.push(item.blogId);
            				return;
            			}
            		}
            		 
            	
            	}
            }
        };
        $scope.saveAddNewBlog = function(){
        	$scope.isAddNewBlog = false;
        	//console.log($scope.addBlog,$scope.cancelBlog)
        	if($scope.addBlog.length != 0){
        		var ids = $scope.addBlog.join(",");
        		updateStationWorks(ids);
        	}
        	if($scope.cancelBlog.length != 0){
            	var id = $scope.cancelBlog.join(',');
                deleteBlog(id)
            }
        };
        $scope.goBlogXq = function(item){
            $state.go('blogxq',{blogid:item.blogId});
        }

    })
});